package com.hnxxxy.common.redis;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/5/23
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@Component
public class ApplicationContentCollection implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContentCollection.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("ApplicationContentCollection类中applicationContext为null");
        }
        return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        return (T) ApplicationContentCollection.getApplicationContext().getBeansOfType(clazz);
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) ApplicationContentCollection.getApplicationContext().getBean(name);
    }

}
